package com.htc.android.footprints.utils.voice;

import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;

public class Recorder implements MediaPlayer.OnCompletionListener,
		MediaPlayer.OnErrorListener {
	public static final int IDLE_STATE = 0;
	public static final int INTERNAL_ERROR = 2;
	public static final int SDCARD_ACCESS_ERROR = 1;
	public static final int NO_ERROR = 0;
	public static final int PAUSE_STATE = 3;
	public static final int PLAYING_STATE = 2;
	public static final int RECORDING_STATE = 1;
	static final String SAMPLE_EXTENSION = ".amr";
	static final String SAMPLE_LENGTH_KEY = "sample_length";
	static final String SAMPLE_PATH_KEY = "sample_path";
	static final String SAMPLE_PREFIX = "footprints";
	static final String TAG = "Recorder";
	OnStateChangedListener mOnStateChangedListener = null;
	MediaPlayer mPlayer = null;
	MediaRecorder mRecorder = null;
	File mSampleFile = null;
	public int mSampleLength = 0;
	long mSampleStart = 0L;
	int mState = 0;

	public Recorder() {
	}

	public Recorder(File paramFile) {
		this.mSampleFile = paramFile;
	}

	public Recorder(String paramString) {
		if (paramString == null)
			;
		while (true) {
			return;
			if (!paramString.equalsIgnoreCase("")) {
				File localFile = new File(paramString);
				if (!localFile.exists())
					continue;
				this.mSampleFile = localFile;
				try {
					MediaPlayer localMediaPlayer = new MediaPlayer();
					localMediaPlayer.reset();
					localMediaPlayer.setDataSource(this.mSampleFile
							.getAbsolutePath());
					localMediaPlayer.prepare();
					this.mSampleLength = localMediaPlayer.getDuration();
					localMediaPlayer.release();
				} catch (IllegalArgumentException localIllegalArgumentException) {
					localIllegalArgumentException.printStackTrace();
				} catch (IllegalStateException localIllegalStateException) {
					localIllegalStateException.printStackTrace();
				} catch (IOException localIOException) {
					localIOException.printStackTrace();
				}
				continue;
			}
		}
	}

	private void setError(int paramInt) {
		if (this.mOnStateChangedListener != null)
			this.mOnStateChangedListener.onError(paramInt);
	}

	private void setState(int paramInt) {
		if (paramInt == this.mState)
			;
		while (true) {
			return;
			this.mState = paramInt;
			signalStateChanged(this.mState);
		}
	}

	private void signalStateChanged(int paramInt) {
		if (this.mOnStateChangedListener != null)
			this.mOnStateChangedListener.onStateChanged(paramInt);
	}

	public void clear() {
		stop();
		this.mSampleLength = 0;
		signalStateChanged(0);
	}

	public void delete() {
		stop();
		if (this.mSampleFile != null)
			this.mSampleFile.delete();
		this.mSampleFile = null;
		this.mSampleLength = 0;
		signalStateChanged(0);
	}

	public long getDuration() {
		long l;
		if (this.mPlayer == null)
			l = -1L;
		while (true) {
			return l;
			l = this.mPlayer.getDuration();
		}
	}

	public int getMaxAmplitude() {
		if (this.mState != 1)
			;
		for (int i = 0;; i = this.mRecorder.getMaxAmplitude())
			return i;
	}

	public void onCompletion(MediaPlayer paramMediaPlayer) {
		setState(0);
		stop();
	}

	public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1,
			int paramInt2) {
		stop();
		setError(1);
		return true;
	}

	public int progress() {
		if ((this.mState == 1) || (this.mState == 2))
			;
		for (int i = (int) (System.currentTimeMillis() - this.mSampleStart);; i = 0)
			return i;
	}

	public void restoreState(Bundle paramBundle) {
		String str = paramBundle.getString("sample_path");
		if (str == null)
			;
		while (true) {
			return;
			int i = paramBundle.getInt("sample_length", -1);
			if (i == -1)
				continue;
			File localFile = new File(str);
			if ((!localFile.exists())
					|| ((this.mSampleFile != null) && (this.mSampleFile
							.getAbsolutePath().compareTo(
									localFile.getAbsolutePath()) == 0)))
				continue;
			delete();
			this.mSampleFile = localFile;
			this.mSampleLength = i;
			signalStateChanged(0);
		}
	}

	public File sampleFile() {
		return this.mSampleFile;
	}

	public int sampleLength() {
		return this.mSampleLength;
	}

	public void saveState(Bundle paramBundle) {
		paramBundle
				.putString("sample_path", this.mSampleFile.getAbsolutePath());
		paramBundle.putInt("sample_length", this.mSampleLength);
	}

	public void setOnStateChangedListener(
			OnStateChangedListener paramOnStateChangedListener) {
		this.mOnStateChangedListener = paramOnStateChangedListener;
	}

	public void startPlayback() {
		stop();
		this.mPlayer = new MediaPlayer();
		try {
			this.mPlayer.setDataSource(this.mSampleFile.getAbsolutePath());
			this.mPlayer.setOnCompletionListener(this);
			this.mPlayer.setOnErrorListener(this);
			this.mPlayer.prepare();
			this.mSampleLength = this.mPlayer.getDuration();
			this.mPlayer.start();
			this.mSampleStart = System.currentTimeMillis();
			setState(2);
			return;
		} catch (IllegalArgumentException localIllegalArgumentException) {
				setError(2);
				this.mPlayer = null;
		} catch (IOException localIOException) {
				setError(1);
				this.mPlayer = null;
		}
	}

	public void startRecording() {
		stop();
		File localFile;
		if (this.mSampleFile == null) {
			localFile = new File(FootprintVoiceManager.getDirectory());
			if (!localFile.exists())
				localFile.mkdirs();
			if (!localFile.canWrite())
				localFile = new File(Environment.getExternalStorageDirectory()
						.toString());
		}
		try {
			this.mSampleFile = File.createTempFile("footprints", ".amr",
					localFile);
			this.mRecorder = new MediaRecorder();
			this.mRecorder.setAudioSource(1);
			this.mRecorder.setOutputFormat(2);
			this.mRecorder.setAudioEncoder(1);
			this.mRecorder.setOutputFile(this.mSampleFile.getAbsolutePath());
		} catch (IOException localIOException1) {
			try {
				this.mRecorder.prepare();
				this.mRecorder.start();
				this.mSampleStart = System.currentTimeMillis();
				setState(1);
				while (true) {
					return;
					localIOException1 = localIOException1;
					setError(1);
				}
			} catch (IllegalStateException localIllegalStateException) {
				while (true)
					localIllegalStateException.printStackTrace();
			} catch (IOException localIOException2) {
				while (true)
					localIOException2.printStackTrace();
			}
		}
	}

	public int state() {
		return this.mState;
	}

	public void stop() {
		stopRecording();
		stopPlayback();
	}

	public void stopPlayback() {
		if (this.mPlayer == null)
			;
		while (true) {
			return;
			this.mPlayer.stop();
			this.mPlayer.release();
			this.mPlayer = null;
			setState(0);
		}
	}

	public void stopRecording() {
		if (this.mRecorder == null)
			;
		while (true) {
			return;
			this.mRecorder.stop();
			this.mRecorder.release();
			this.mRecorder = null;
			this.mSampleLength = (int) (System.currentTimeMillis() - this.mSampleStart);
			setState(0);
		}
	}

	public static abstract interface OnStateChangedListener {
		public abstract void onError(int paramInt);

		public abstract void onStateChanged(int paramInt);
	}
}

/*
 * Location:
 * E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name: com.htc.android.footprints.utils.voice.Recorder JD-Core
 * Version: 0.6.0
 */